package cn.bdqn.util;

import cn.bdqn.entity.AuthorityUser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: springSecuritySeparate
 * @Package: cn.bdqn.util
 * @Author: huat
 * @Date: 2019/12/24 10:58
 * @Version: 1.0
 */
@Slf4j
public class JsonUtil {
    public static final ObjectMapper objectMapper=new ObjectMapper();

    /**
     * 将对象转为json
     * @param object  对象
     * @return json
     */
    public static String toString(Object object) {
        if(null==object){
            return null;
        }
        if(object.getClass()==String.class){
            return (String)object;
        }
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            log.info(object+"序列化出错"+e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将json转换成对象
     * @param json json字符串
     * @param tClass 类
     * @param <T> 泛型
     * @return 返回对象
     */
    public static <T> T toBean(String json,Class<T> tClass){
        try {
            return objectMapper.readValue(json,tClass);
        } catch (IOException e) {
            log.info(json+"转对象失败"+e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 将json转换成集合对象
     * @param json json字符串
     * @param eClass 类 集合对象
     * @param <E> 泛型
     * @return 返回对象
     */
    public static <E> List<E> toList(String json,Class<E> eClass){
        try {
            return objectMapper.readValue(json,objectMapper.getTypeFactory().constructCollectionType(List.class,eClass));
        } catch (IOException e) {
            log.info(json+"转对象失败"+e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将json转换成Map对象
     * @param json json字符串
     * @param kClass 键对象
     * @param vClass 值对象
     * @param <K> 键的类型
     * @param <V> 值的类型
     * @return map对象
     */
    public static <K,V> Map<K,V> toMap(String json,Class<K> kClass,Class<V> vClass){
        try {
            return objectMapper.readValue(json,objectMapper.getTypeFactory().constructMapType(Map.class,kClass,vClass));
        } catch (IOException e) {
            log.info(json+"转对象失败"+e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 复杂类型转换  泛型套泛型
     * @param json json字符串
     * @param type  返回类型
     * @param <T>
     * @return 返回对象
     */
    public static <T> T nativeRead(String json, TypeReference<T> type){
        try {
            return objectMapper.readValue(json,type);
        } catch (IOException e) {
            log.info(json+"转对象失败"+e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
      //测试nativeRead方法
      String json="{\"id\":1,\"username\":\"张三\",\"password\":\"123\",\"authorityRoles\":[{\"rid\":0,\"roleName\":\"张三0\",\"roleNameCN\":null},{\"rid\":1,\"roleName\":\"张三1\",\"roleNameCN\":null},{\"rid\":2,\"roleName\":\"张三2\",\"roleNameCN\":null},{\"rid\":3,\"roleName\":\"张三3\",\"roleNameCN\":null},{\"rid\":4,\"roleName\":\"张三4\",\"roleNameCN\":null}]}\n";
      JsonUtil.nativeRead(json,new TypeReference<AuthorityUser>(){});

    }
}
